package com.bckj.fastboot.core.util;

import lombok.extern.slf4j.Slf4j;

import java.sql.Timestamp;
import java.text.NumberFormat;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class ConvertUtils {
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final Pattern UNDERSCORE_PATTERN = Pattern.compile("_(\\w)");

    /**
     * 驼峰转下划线
     *
     * @param camelCaseStr 驼峰字符串
     * @return 下划线字符串
     */
    public static String camelCaseToUnderline(String camelCaseStr) {
        if (camelCaseStr == null || camelCaseStr.isBlank()) {
            return camelCaseStr;
        }
        // 使用正则表达式匹配驼峰命名中的大写字母，并在其前面插入下划线
        // 注意：正则表达式中的"(?<!^)"是一个负向后查找，用于确保不在字符串开头插入下划线
        return camelCaseStr.replaceAll("(?<!^)([A-Z])", "_$1").toLowerCase();
    }

    public static String underlineToCamelCase(String underlineStr) {
        if (underlineStr == null || underlineStr.isEmpty()) {
            return underlineStr;
        }

        Matcher matcher = UNDERSCORE_PATTERN.matcher(underlineStr.toLowerCase());
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static Number parseNumber(Object answer) {
        if (answer == null) {
            return null;
        }
        if (answer instanceof Number) {
            return (Number) answer;
        }

        if (answer instanceof String) {
            try {
                String text = (String) answer;
                return NumberFormat.getInstance().parse(text);
            } catch (ParseException var4) {
                log.error("parseNumber error: " + var4.getMessage());
            }
        }
        return null;
    }

    public static Boolean parseBoolean(Object answer) {
        if (answer == null) {
            return null;
        }
        if (answer instanceof Boolean) {
            return (Boolean)answer;
        }
        if (answer instanceof Number number) {
            return number.intValue() != 0;
        }
        if (answer instanceof String text) {
            return "true".equalsIgnoreCase(text) || "1".equals(text);
        }
        return null;
    }

    public static LocalDateTime parseLocalDateTime( Object answer) {
        if (answer == null) {
            return null;
        }
        if (answer instanceof LocalDateTime) {
            return (LocalDateTime) answer;
        }
        if (answer instanceof Timestamp timestamp) {
            return timestamp.toLocalDateTime();
        }
        if (answer instanceof String) {
            return LocalDateTime.parse((String) answer, DATE_TIME_FORMATTER);
        }
        return LocalDateTime.parse(answer.toString(), DATE_TIME_FORMATTER);
    }

    public static LocalDate parseLocalDate(Object answer) {
        if (answer == null) {
            return null;
        }
        if (answer instanceof LocalDate) {
            return (LocalDate) answer;
        }
        if (answer instanceof java.sql.Date date) {
            return date.toLocalDate();
        }
        if (answer instanceof String) {
            return LocalDate.parse((String) answer, DateTimeFormatter.ISO_DATE);
        }
        return LocalDate.parse(answer.toString(), DateTimeFormatter.ISO_DATE);
    }


    public static LocalTime parseLocalTime(Object answer) {
        if (answer == null) {
            return null;
        }
        if (answer instanceof LocalTime) {
            return (LocalTime) answer;
        }
        return LocalTime.parse(answer.toString());
    }
}

